﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Shop.Wares.Models;
using Shop.Page.Services;
using System.IO;
using Shop.Page.Models;

namespace Shop.Page.Page
{
    public class PageController : Controller
    {
        Shop.Home.Services.HomeAccess hbll = new Home.Services.HomeAccess();
        Shop.Page.Services.PageAccess bll = new PageAccess();
        Shop.Page.Services.HomeBottomAccess hhbll = new HomeBottomAccess();
        Shop.Page.Services.OtherAccess obll = new OtherAccess();

        /// <summary>
        /// 零散页面管理
        /// </summary>
        /// <returns></returns>
        public ActionResult PagesManage()
        {
            IList<Models.WaresPicture> wpinfo = bll.GetWPInfo();
            return View(wpinfo);
        }
        /// <summary>
        /// 添加页面
        /// </summary>
        /// <returns></returns>
        public ActionResult AddPage()
        {
            return View();
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="FC"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddPage(FormCollection FC, HttpPostedFileBase Url)
        {

            string FullFileUrl = "";
            //接收自己单独要上传的图片            
            if (Url != null)
            {
                string RndFileName = string.Format("{0}{1}{2}", 
                    DateTime.Now.ToString("yyMMdd"),
                    Path.GetRandomFileName()
                    .Replace(".", string.Empty), 
                    Path.GetExtension(Url.FileName));
                FullFileUrl = string.Format("/upload/{0}", 
                    RndFileName);
                string SaveFilePathName = Server
                    .MapPath(FullFileUrl);
                if (!YuHou.ReUse.ImageOperation
                    .IntelligentSaveImageFile(Url.InputStream
                    , SaveFilePathName, 0, true, ""
                    , new System.Drawing.Size(500, 500)))
                    Url.SaveAs(SaveFilePathName);
            }
            int WID;
            int.TryParse(FC["WID"], out WID);
            string WName = FC["WName"];
            int Type;
            int.TryParse(FC["Type"], out Type);
           WaresPicture wpinfo = new WaresPicture();
            wpinfo.WID = WID;
            wpinfo.WName = WName;
            wpinfo.Url = FullFileUrl;
            wpinfo.Type = Type;
            wpinfo.Createdate = DateTime.Now;
            wpinfo.OrderTime = DateTime.Now;

            try
            {
                bll.AddWares(wpinfo);
            }
            catch (Exception)
            {
                var scripts = ("<script>alert('添加失败');document.location.href = 'PagesManage'; </script>");
                return Content(scripts);
            }
            var scripts1 = ("<script>alert('添加成功');document.location.href = 'PagesManage'; </script>");
            return Content(scripts1);
        }
        /// <summary>
        /// 判断商品是否存在
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult JudgeWares()
        {
            int WID;
            int.TryParse(Request.Form["WID"], out WID);
            Shop.Wares.Models.AnyWares winfo = hbll.GetWInfo(WID);
            if (winfo == null)
            {
                return Content("NO");
            }
            if (winfo.Status == 1)
            {
                return Content("NO");
            }
            return Content(winfo.WName);
        }
        /// <summary>
        /// 修改界面
        /// </summary>
        /// <param name="HID"></param>
        /// <returns></returns>

        public ActionResult UpdatePage(int HID)
        {
            Models.WaresPicture wpinfo = bll.GetWPInfoByHID(HID);
            return View(wpinfo);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="FC"></param>
        /// <param name="Url"></param>
        /// <returns></returns>
        [HttpPost]

        public ActionResult UpdatePage(FormCollection FC, HttpPostedFileBase Url)
        {
            string FullFileUrl = "";
            //接收自己单独要上传的图片            
            if (Url != null)
            {
                string RndFileName = string.Format("{0}{1}{2}", DateTime.Now.ToString("yyMMdd"), Path.GetRandomFileName().Replace(".", string.Empty), Path.GetExtension(Url.FileName));
                FullFileUrl = string.Format("/upload/{0}", RndFileName);
                string SaveFilePathName = Server.MapPath(FullFileUrl);
                if (!YuHou.ReUse.ImageOperation.IntelligentSaveImageFile(Url.InputStream, SaveFilePathName, 0, true, "", new System.Drawing.Size(500, 500)))
                    Url.SaveAs(SaveFilePathName);

                //EditorContents += "上传图片成功！"; //上传成功提示，不需要可以删除。
            }
            else
            {
                FullFileUrl = FC["OldUrl"];
            }
            int HID;
            int.TryParse(FC["HID"], out HID);
            int WID;
            int.TryParse(FC["WID"], out WID);
            string WName = FC["WName"];
            int Type;
            int.TryParse(FC["Type"], out Type);
            Shop.Page.Models.WaresPicture wpinfo = new Models.WaresPicture();
            wpinfo.HID = HID;
            wpinfo.WID = WID;
            wpinfo.WName = WName;
            wpinfo.Url = FullFileUrl;

            if (bll.UpdatePage(wpinfo) == 0)
            {
                var scripts = ("<script>alert('修改失败');document.location.href = 'PagesManage'; </script>");
                return Content(scripts);
            }
            var scripts1 = ("<script>alert('修改成功');document.location.href = 'PagesManage'; </script>");
            return Content(scripts1);
        }
        /// <summary>
        ///置顶
        /// </summary>
        /// <returns></returns>
        public ActionResult OrderPage()
        {
            int HID;
            int.TryParse(Request.Form["HID"], out HID);
            if (bll.OrderPage(HID) == 0)
            {
                return Content("NO");
            }
            return Content("OK");
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        public ActionResult DelPage()
        {
            int HID;
            int.TryParse(Request.Form["HID"], out HID);
            if (bll.DelPage(HID) == 0)
            {
                return Content("NO");
            }
            return Content("OK");
        }

        #region 底部链接
        public ActionResult HomeBottomManage()
        {
            IList<HomeBottom> hinfo = hhbll.GetHomeBottomInfoByType(0);
            return View(hinfo);
        }
        public ActionResult _HomeBottom(int Superior)
        {
            IList<HomeBottom> hinfo = hhbll.GetHomeBottomInfoBySuperior(Superior);
            return View(hinfo);
        }
        /// <summary>
        /// 添加链接
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddHomeBottomInfo()
        {
            string HBName = Request.Form["HBName"];
            string HBHref = Request.Form["HBHref"];
            int Superior = Convert.ToInt32(Request.Form["Superior"]);
            HomeBottom hinfo = new HomeBottom();
            hinfo.HBName = HBName;
            hinfo.HBHref = HBHref;
            hinfo.Superior = Superior;
            hinfo.Type = 1;
            hinfo.OrderTime = DateTime.Now;
            hinfo.Createdate = hinfo.OrderTime;
            try
            {
                hhbll.AddHomeBottomInfo(hinfo);
            }
            catch (Exception)
            {
                var scripts = ("<script>alert('添加失败');document.location.href = 'HomeBottomManage'; </script>");
                return Content(scripts);
            }
            var script = ("<script>alert('添加成功');document.location.href = 'HomeBottomManage'; </script>");
            return Content(script);

        }

        /// <summary>
        /// 修改链接
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateHomeBottomInfo()
        {
            string HBName = Request.Form["UHBName"];
            string HBHref = Request.Form["UHBHref"];
            int Superior = Convert.ToInt32(Request.Form["USuperior"]);
            int HBID = Convert.ToInt32(Request.Form["HBID"]);
            HomeBottom hinfo = new HomeBottom();
            hinfo.HBID = HBID;
            hinfo.HBName = HBName;
            hinfo.HBHref = HBHref;
            hinfo.Superior = Superior;
            hinfo.Type = 1;
            hinfo.OrderTime = DateTime.Now;
            hinfo.Createdate = hinfo.OrderTime;
            if (hhbll.UpdateHomeBottomInfo(hinfo) == 0)
            {
                var scripts = ("<script>alert('修改失败');document.location.href = 'HomeBottomManage'; </script>");
                return Content(scripts);
            }
            var script = ("<script>alert('修改成功');document.location.href = 'HomeBottomManage'; </script>");
            return Content(script);

        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DelHomeBottomInfo()
        {
            int HBID = Convert.ToInt32(Request.Form["HBID"]);
            if (hhbll.DelHomeBottomInfo(HBID) == 0)
            {
                return Content("NO");
            }
            return Content("OK");
        }

        /// <summary>
        ///置顶
        /// </summary>
        /// <returns></returns>
        public ActionResult OrderHomeBottomInfo()
        {
            int HBID = Convert.ToInt32(Request.Form["HBID"]);
            if (hhbll.Order(HBID) == 0)
            {
                return Content("NO");
            }
            return Content("OK");
        }
        #endregion


        #region 其他
        /// <summary>
        /// 其他管理界面
        /// </summary>
        /// <returns></returns>
        public ActionResult OtherManage()
        {
            IList<Other> oinfo = obll.GetOtherInfo();
            return View(oinfo);
        }
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateOtherInfo()
        {
            int Type = Convert.ToInt32(Request.Form["Type"]);
            string Value = Request.Form["Value"];
            Other oinfo = new Other();
            oinfo.Type = Type;
            oinfo.Value = Value;
            if (obll.UpdateOtherInfo(oinfo) == 0)
            {
                var scripts = ("<script>alert('修改失败');document.location.href = 'OtherManage'; </script>");
                return Content(scripts);
            }
            var script = ("<script>alert('修改成功');document.location.href = 'OtherManage'; </script>");
            return Content(script);

        }
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult UpdateLogoInfo(HttpPostedFileBase LogoUrl)
        {
            string FullFileUrl = "";
            //接收自己单独要上传的图片            
            if (LogoUrl != null)
            {
                string RndFileName = string.Format("{0}{1}{2}", DateTime.Now.ToString("yyMMdd"), Path.GetRandomFileName().Replace(".", string.Empty), Path.GetExtension(LogoUrl.FileName));
                FullFileUrl = string.Format("/upload/{0}", RndFileName);
                string SaveFilePathName = Server.MapPath(FullFileUrl);
                if (!YuHou.ReUse.ImageOperation.IntelligentSaveImageFile(LogoUrl.InputStream, SaveFilePathName, 0, true, "", new System.Drawing.Size(500, 500)))
                    LogoUrl.SaveAs(SaveFilePathName);

                //EditorContents += "上传图片成功！"; //上传成功提示，不需要可以删除。
            }
            Other oinfo = new Other();
            oinfo.Type = 5;
            oinfo.Value = FullFileUrl;
            if (obll.UpdateOtherInfo(oinfo) == 0)
            {
                var scripts = ("<script>alert('修改失败');document.location.href = 'OtherManage'; </script>");
                return Content(scripts);
            }
            var script = ("<script>alert('修改成功');document.location.href = 'OtherManage'; </script>");
            return Content(script);
        }
        #endregion
    }

}